#The name of the project.
set(PROJECTNAME PS_Praktikum2)
PROJECT(${PROJECTNAME})

#-----------------------------DEBUG / RELEASE-------------------------------------#
set(CMAKE_BUILD_TYPE Debug)
#-------------------------END DEBUG / RELEASE-------------------------------------#


#-----------------------------OPTIMIZATION-------------------------------------#
#compiler optimizations
set(CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE} "-O3")
set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE} "-O3")
#-------------------------END OPTIMIZATION-------------------------------------#

#------------------------------OPENMP-------------------------------------#
include(FindOpenMP)

if(OPENMP_FOUND)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
#----------------------------END OPENMP-----------------------------------#


#------------------------------CBLAS-------------------------------------#
#find all BLAS libraries
file(GLOB blaslibs
    /usr/lib/libblas/*.so
)
#----------------------------END CBLAS-----------------------------------#

#------------------------------ATLAS-------------------------------------#
#find all ATLAS libraries
file(GLOB atlaslibs
    /usr/local/atlas/lib/libsatlas.so
)
#----------------------------END ATLAS-----------------------------------#

#----------------------------CUDA -----------------------------------#
#CMake Minimum verion required. FindCUDA script
#is distributed since version 2.8
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

#Include FindCUDA script
INCLUDE(FindCUDA)

#Search CUDA.
FIND_PACKAGE(CUDA)

#save path to CUDA and CUDA SDK in variables $CUDA_ROOT_DIR and $CUDA_SDK_ROOT_DIR
set(CUDA_SDK_ROOT_DIR /home/username/NVIDIA_GPU_Computing_SDK)
set(CUDA_ROOT_DIR /usr/local/cuda)

#debug printout to check paths variables
message("Debug printout of directory variables:")
message("{CUDA_SDK_ROOT_DIR}: ${CUDA_SDK_ROOT_DIR}")
message("{CUDA_ROOT_DIR}: ${CUDA_ROOT_DIR}")


#find all cuda libraries
file(GLOB cudalibs
    ${CUDA_ROOT_DIR}/lib64/*.so
    #${CUDA_ROOT_DIR}/lib/*.so
)

set(cudalibs
    ${cudalibs}
    ${CUDA_SDK_ROOT_DIR}/C/lib/libcutil_x86_64.a
    ${CUDA_SDK_ROOT_DIR}/shared/lib/libshrutil_x86_64.a
)

#cuda include folders
INCLUDE_DIRECTORIES(${CUDA_SDK_ROOT_DIR}/CUDALibraries/common/inc)
INCLUDE_DIRECTORIES(${CUDA_SDK_ROOT_DIR}/C/common/inc)
INCLUDE_DIRECTORIES(${CUDA_SDK_ROOT_DIR}/C/src)
INCLUDE_DIRECTORIES(${CUDA_SDK_ROOT_DIR}/shared/inc)
INCLUDE_DIRECTORIES(${CUDA_ROOT_DIR}/include)
#------------------------- END CUDA -----------------------------------#


#-------------------------COMPUTE CAPABILITY---------------------------#
set(CUDA_NVCC_FLAGS "-arch=sm_30 -O")
#---------------------END COMPUTE CAPABILITY---------------------------#



#-------------------------ADD FILES TO PROJECT---------------------------#
#scan all h, cpp and cu files in project folder
file(GLOB headers
        *.h
)

file(GLOB sources
        *.cpp
)

file(GLOB cudaSources
        *.cu
)

file(GLOB kernels
        *kernel.cu
	*Kernel.cu
)

#ignore kernel files for compiler
SET_SOURCE_FILES_PROPERTIES(${kernels} PROPERTIES HEADER_FILE_ONLY TRUE)

#compile all .cpp and .cu files
CUDA_ADD_EXECUTABLE(${PROJECTNAME} ${headers} ${sources} ${cudaSources})
#---------------------END ADD FILES TO PROJECT---------------------------#


#-------------------------TARGET LIBRARIES---------------------------#
#link with libs
#TARGET_LINK_LIBRARIES(${PROJECTNAME} ${cudalibs} "-ldl")
#TARGET_LINK_LIBRARIES(${PROJECTNAME} ${blaslibs})
TARGET_LINK_LIBRARIES(${PROJECTNAME} ${atlaslibs})
#---------------------END TARGET LIBRARIES---------------------------#

